// pages/minenewslist/minenewslist.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    newslistflag: true,
    showLoading: true,
    all_news_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.tit) {
      wx.setNavigationBarTitle({
        title: options.tit
      })
    }
    this.setData({
      currentindex: options.index //2-世瀚活动；3-合伙人消息；4-优惠券消息
    })
    this.changenewslist(this.data.currentindex)
  },
  changenewslist: function(index) {
    switch (Number(index)) { //get_activity_message  get_message_center  get_salesrule_message     
      case 2:
        this.refreshnewslist(config.get_activity_message)
        break;
      case 3:
        this.refreshnewslist(config.get_message_center)
        break;
      case 4:
        this.refreshnewslist(config.get_salesrule_message)
        break;
    }
  },
  refreshnewslist: function(param) {
    var that = this
    shihan.sendRequest(param, 'POST', { page: that.data.page, page_size: that.data.page_size })
      .then(function(response) {
        if (response.data.code == 0) {
          if (that.data.page == 1) {
            var messageids = wx.getStorageSync('messageids') || '';
            //{"sh_activity_id":230,"sh_partner_inform_id":1,"sh_customer_salesrule_id":0}
            if (response.data.data.sh_items.length > 0) {
              if (that.data.currentindex == 1) {
                wx.setStorage({
                  key: 'messageids',
                  data: {
                    "sh_activity_id": response.data.data.sh_items[0].sh_id,
                    "sh_partner_inform_id": messageids.sh_partner_inform_id,
                    "sh_customer_salesrule_id": messageids.sh_customer_salesrule_id
                  }
                })
              } else if (that.data.currentindex == 3) {
                wx.setStorage({
                  key: 'messageids',
                  data: {
                    "sh_activity_id": messageids.sh_activity_id,
                    "sh_partner_inform_id": messageids.sh_partner_inform_id,
                    "sh_customer_salesrule_id": response.data.data.sh_items[0].sh_id
                  }
                })
              }
            } else {
              if (that.data.currentindex == 1) {
                wx.setStorage({
                  key: 'messageids',
                  data: {
                    "sh_activity_id": 0,
                    "sh_partner_inform_id": messageids.sh_partner_inform_id,
                    "sh_customer_salesrule_id": messageids.sh_customer_salesrule_id
                  }
                })
              } else if (that.data.currentindex == 3) {
                wx.setStorage({
                  key: 'messageids',
                  data: {
                    "sh_activity_id": messageids.sh_activity_id,
                    "sh_partner_inform_id": messageids.sh_partner_inform_id,
                    "sh_customer_salesrule_id": 0
                  }
                })
              }
            }
          }
          that.setData({
            page: that.data.page + 1,
            newsinfo: response.data.data,
            all_news_list: that.data.all_news_list.concat(response.data.data.sh_items),
            newslistflag: true,
            showLoading: false
          })
          if (response.data.data.sh_total_rows <= that.data.page_size || response.data.data.sh_total_rows == that.data.all_news_list.length) {
            that.setData({
              newslistflag: false,
              showLoading: false
            })
          }
          wx.stopPullDownRefresh()
        } else {
          that.setData({
            newslistflag: false,
            showLoading: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  opendetail: function(e) {
    if (e.currentTarget.dataset.url) {
      wx.navigateTo({
        url: '/pages/strategyinfo/strategyinfo?name=&url=' + encodeURIComponent(e.currentTarget.dataset.url),
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      all_news_list: [],
      showLoading: true
    });
    if (this.data.all_news_list == 0) {
      this.changenewslist(this.data.currentindex)
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.newslistflag) {
      this.setData({
        newslistflag: false
      })
      this.changenewslist(this.data.currentindex)
    }
  }
})